<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
		<!-- 导入 JQuery -->
		<script src="/assets/lib/jquery.js"></script>
	</head>
	<body>
		<div id="box"></div>
		<button id="btn">刷新</button>
		<input type="text" id="ipt" />
		<span id="span"></span>
		<script>
			const box = document.querySelector('#box')
			const btn = document.querySelector('#btn')
			const ipt = document.querySelector('#ipt')
			const span = document.querySelector('#span')
			// 存放正确的验证码文本
			let validation = ''
			// 获取验证码图片和文本
			function getInfo() {
				$.get('http://127.0.0.1:8080/api/getCaptcha', (res) => {
					// 正确验证码的信息
					validation = res.text
					// res.data是一个验证码图片 svg标签
					box.innerHTML = res.data
				})
			}
			getInfo()
			btn.addEventListener('click', getInfo)
			ipt.addEventListener('blur', function (e) {
				if (this.value.toLowerCase() === validation.toLowerCase()) {
					span.innerHTML = '验证通过'
					span.style.color = 'green'
				} else {
					span.innerHTML = '验证不通过'
					span.style.color = 'red'
				}
			})
		</script>
	</body>
</html>
